//
//  ReportView.m
//  deerkids
//
//  Created by zhangxy on 2017/11/22.
//  Copyright © 2017年 wangzhao. All rights reserved.
//

#import "ReportView.h"

#import "MyButton.h"

#import "UIView+Border.h"

#import "ZTextView.h"

#define CHeight 346

#define CWidth SCREEN_WIDTH - 100


@interface ReportView()<UITextViewDelegate>{
    MyButton *checkBtn;
    CGFloat _keyboardHeight;
    NSArray *arr;
}

@property(nonatomic,strong) UIViewController *curVC;

@property(nonatomic,strong) UIView *bottomView;
@property(nonatomic,strong) ZTextView *chatTextView;

@end


@implementation ReportView

-(instancetype)initWithShareType:(UIViewController *)vc{
    self = [super initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT)];
    if (self)
    {
        _curVC = vc;
        self.backgroundColor = [UIColor clearColor];
        arr = @[@"辱骂",@"色情",@"广告"];
        
        [[UIApplication sharedApplication].keyWindow addSubview:self];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dissmisMenu)];
        [self addGestureRecognizer:tap];
        
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(50, SCREEN_HEIGHT-CHeight, CWidth, CHeight)];
        _bottomView.backgroundColor = UIColor.whiteColor;
        _bottomView.layer.cornerRadius = 5.0f;
        _bottomView.layer.masksToBounds = YES;
        [self addSubview:_bottomView];
        
        UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dissmisMenu)];
        [_bottomView addGestureRecognizer:tap1];
        
        UILabel *labTitle = [UILabel new];
        [labTitle setFrame:CGRectMake(15, 15, SCREEN_WIDTH - 30, 21)];
        [labTitle setFont:FontFiftent];
        [labTitle setTextColor:UIColorFromRGB(TextSizeOneColor)];
        [labTitle setText:@"请选择理由"];
        [_bottomView addSubview:labTitle];
        
        
        [self loadReportList];
        [self chatTextView];
        [self createLastView];
        
        [self handleKeyboard];
        
        
    }
    
    return self;
}
-(void)createLastView{
    CGFloat y = CGRectGetMaxY(self.chatTextView.frame) + 10;
    [self createButtonClick:1 y:y];
    [self createButtonClick:2 y:y+44];
}

-(void)createButtonClick:(int) i y:(CGFloat) y{
    UIButton *btnClose = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnClose setFrame:CGRectMake(0, y, CWidth, 44)];
    if(i == 1){
        [btnClose setTitle:@"确定" forState:0];
        [btnClose addTarget:self action:@selector(actionButton:) forControlEvents:UIControlEventTouchUpInside];
    }else{
        [btnClose setTitle:@"取消" forState:0];
        [btnClose addTarget:self action:@selector(dissmisMenu) forControlEvents:UIControlEventTouchUpInside];
    }
    [btnClose setBackgroundColor:UIColor.whiteColor];
    [btnClose setTitleColor:UIColorFromRGB(Text6Color) forState:0];
    btnClose.layer.masksToBounds = YES;
    [btnClose addTopBorderWithColor:UIColorFromRGB(BgLineColor) andWidth:1.0f];
    [_bottomView addSubview:btnClose];
    
}

-(void)loadReportList{
    int i = 1;
    for (NSString *item in arr) {
        
        [self createShareButton:i title:item];
        i ++;
    }
}

-(void)createShareButton:(int) i title:(NSString *) title{
    CGFloat y = 51 + 50*(i-1);
    CGFloat itemW = CWidth - 30;
    CGFloat x = 15;
    MyButton *btn = [MyButton buttonWithType:UIButtonTypeCustom];
    [btn setFrame:CGRectMake(x, y, itemW, 50)];
    [btn setTitle:title forState:0];
    [btn setImageEdgeInsets:UIEdgeInsetsMake(15, 0, 5, 15)];
    [btn.imageView setContentMode:UIViewContentModeScaleAspectFit];
    [btn setTitleColor:UIColorFromRGB(TextSizeOneColor) forState:UIControlStateNormal];
    [btn.titleLabel setFont:FontFiftent];
    [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [btn addTarget:self action:@selector(shareWithButton:) forControlEvents:UIControlEventTouchUpInside];
    [_bottomView addSubview:btn];
    [btn setImage:[UIImage imageNamed:@"ic_check_unselect"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"ic_check_select"] forState:UIControlStateSelected];
    btn.tag = i;
}

-(ZTextView *)chatTextView{
    if (!_chatTextView) {
        _chatTextView = [[ZTextView alloc] initWithFrame:CGRectMake(15, 210 ,CWidth -30, 38)];
        [_chatTextView setPlaceholder:@"不充分 自己写"];
        _chatTextView.layer.cornerRadius = 5;
        _chatTextView.layer.masksToBounds = YES;
        if (iOS7) {
            _chatTextView.layoutManager.allowsNonContiguousLayout = NO;
        }
        
        _chatTextView.placeholderColor = LIGHT_GRAY_COLOR;
        _chatTextView.textColor = UIColorFromRGB(TextSizeEightColor);
        _chatTextView.font = FontFiftent;
        _chatTextView.returnKeyType = UIReturnKeySend;
        _chatTextView.autoresizesSubviews = YES;
        _chatTextView.delegate = self;
        _chatTextView.textAlignment = NSTextAlignmentLeft;
        _chatTextView.autoresizingMask = (UIViewAutoresizingFlexibleWidth);
        [_chatTextView setBackgroundColor:UIColorFromRGB(0xF6F6F6)];
        [_bottomView addSubview:_chatTextView];
        
    }
    return _chatTextView;
}

-(void)shareWithButton:(MyButton *) shareButton{
    if(checkBtn){
        checkBtn.selected = NO;
    }
    shareButton.selected = YES;
    checkBtn = shareButton;
}


-(void)actionButton:(UIButton *) btn{
    
    if(checkBtn == nil && convertToString(_chatTextView.text).length==0){
        [PopUtil showMessage:@"请选择或输入举报内容"];
        return;
    }
   
    NSDictionary *p = @{@"session_uid":[UserUtil getUser].user_id,
                        @"report_type":convertToString(_report_type),
                        @"report_reason":convertToString(checkBtn.titleLabel.text),
                        @"reason_custom":convertToString(_chatTextView.text),
                        @"mapping_id":convertToString(_mapping_id),
                        @"mapping_uid":convertToString(_mapping_uid)
                        };
    [NetworkUtil POST:API_ReportSend parameters:p success:^(id responseObject, NSDictionary *dict) {
        [PopUtil showMessage:@"举报成功"];
        [self dissmisMenu];
        
    } failure:^(NSError *error, NSString *message) {
        [PopUtil showMessage:message];
    }];
}


- (void)dissmisMenu{
    if(_keyboardHeight>0){
        [self hideKeyboard];
        return;
    }
    
    CGRect sheetViewF = self.frame;
    sheetViewF.origin.y = SCREEN_HEIGHT;
    self.backgroundColor = UIColor.clearColor;
    
    [UIView animateWithDuration:0.2 animations:^{
        
        self.alpha = 0.0;
        self.frame = sheetViewF;
        
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(void)show{
    CGRect sheetViewF = self.frame;
    sheetViewF.origin.y = 0;
    self.frame = sheetViewF;
    
    CGRect bf = _bottomView.frame;
    bf.origin.y = ScreenHeight/2 - CHeight/2;
    [UIView animateWithDuration:0.3 animations:^{
        _bottomView.frame = bf;
        self.backgroundColor = UIColorFromRGBAlpha(TextBlackColor, 0.4);
    } completion:^(BOOL finished) {
        
    }];
}



- (void)handleKeyboard{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

-(void)removeKeyboardObserver{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}


//键盘显示
- (void)keyboardWillShow:(NSNotification *)notification {
    float animationDuration = [[[notification userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    _keyboardHeight = [[[notification userInfo] objectForKey:@"UIKeyboardBoundsUserInfoKey"] CGRectValue].size.height;
    
    
    NSNumber *curve = [notification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:animationDuration];
    [UIView setAnimationCurve:[curve intValue]];
    [UIView setAnimationDelegate:self];
    // get a rect for the view frame
    
    {
//        CGRect bf         = _bottomView.frame;
//
//
//
//       bf.origin.y =  (SCREEN_HEIGHT - bf.size.height) - _keyboardHeight;
//       _bottomView.frame = bf;
    }
    
    // commit animations
    [UIView commitAnimations];
}


#pragma mark 键盘隐藏 keyboard notification
-(void)hideKeyboard{
    [_chatTextView resignFirstResponder];
}
//键盘隐藏
- (void)keyboardWillHide:(NSNotification *)notification {
    float animationDuration = [[[notification userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    [UIView beginAnimations:@"bottomBarDown" context:nil];
    [UIView setAnimationDuration:animationDuration];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    
    _keyboardHeight = 0;
    
    [UIView commitAnimations];
    
    [UIView animateWithDuration:0.25 animations:^{
//        CGRect bf = _bottomView.frame;
//        bf.origin.y = (SCREEN_HEIGHT - bf.size.height);
//        _bottomView.frame = bf;
        
    }];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
